﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Call_Center_Gochi.DAL;

namespace Call_Center_Gochi.Cliente
{
    public partial class FCliente : Form
    {
        Conexion Cn = new Conexion();
        BindingSource bindingsource1;
        Boolean Existe = false;
        Int64 idcliente = 0;
        string Usuario;
        Origen O;

        public FCliente(String Usuario)
        {
            InitializeComponent();
            this.Usuario = Usuario;
            FillGrid();
            TipoCliente();
            TipoOrigen();
            TipoOperador();
            Filtro1();
            O = null;
        }

        public void FillGrid()
        {
            bindingsource1 = new BindingSource();
            bindingsource1.DataSource = Cn.DarClientes();
            this.dataGridView1.DataSource = bindingsource1;
            this.dataGridView1.Columns["idCliente"].Visible = false;
            this.dataGridView1.Columns["Tipo_Cliente_idTipo_Cliente"].Visible = false;
            this.dataGridView1.Columns["Origen_idOrigen"].Visible = false;
            this.dataGridView1.Columns["Operador_idOperador"].Visible = false;
            
        }

        public void FillGrid2()
        {
            this.dataGridView2.DataSource = Cn.DarGestion(idcliente);
            this.dataGridView2.Columns["idGestion"].Visible = false;
            this.dataGridView2.Columns["cliente_idCliente"].Visible = false;
            this.dataGridView2.Columns["usuario_idUsuario"].Visible = false;
            this.dataGridView2.Columns["estado_idEstado"].Visible = false;
        }

        public void FillGrid3(int num)
        {
            switch (num){
                case 1:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeNombre(this.textBox1.Text);
                        break;
                case 2:
                        this.dataGridView1.DataSource = Cn.DarClientesLike1erApellido(this.textBox1.Text);
                        break;
                case 3:
                        this.dataGridView1.DataSource = Cn.DarClientesLike2doApellido(this.textBox1.Text);
                        break;
                case 4:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeCiudad(this.textBox1.Text);
                        break;
                case 5:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeBarrio(this.textBox1.Text);
                        break;
                case 6:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeTelfijo(this.textBox1.Text);
                        break;
                case 7:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeTelMovil(this.textBox1.Text);
                        break;
                case 8:
                        this.dataGridView1.DataSource = Cn.DarClientesLikeEmail(this.textBox1.Text);
                        break;
            }

            this.dataGridView1.Columns["idCliente"].Visible = false;
            this.dataGridView1.Columns["Tipo_Cliente_idTipo_Cliente"].Visible = false;
            this.dataGridView1.Columns["Origen_idOrigen"].Visible = false;
            this.dataGridView1.Columns["Operador_idOperador"].Visible = false;

        }

        public void TipoCliente()
        {
            int cont = 0;
            List<String> TipoCliente = Cn.TipoCliente();
            this.comboBox1.Items.Clear();
            this.comboBox1.Text = "Seleccione Tipo de Cliente";
            this.comboBox1.Items.Insert(cont, "Seleccione Tipo de Cliente");
            cont++;
            foreach (String Tipo in TipoCliente)
            {
                this.comboBox1.Items.Insert(cont, Tipo);
                cont++;
            }
        }

        public void TipoOrigen()
        {
            int cont = 0;
            List<String> TipoOrigen = Cn.TipoOrigen();
            this.comboBox2.Items.Clear();
            this.comboBox2.Text = "Seleccione Tipo de Origen";
            this.comboBox2.Items.Insert(cont, "Seleccione Tipo de Origen");
            cont++;
            foreach (String Tipo in TipoOrigen)
            {
                this.comboBox2.Items.Insert(cont, Tipo);
                cont++;
            }
        }

        public void TipoOperador()
        {
            int cont = 0;
            List<String> TipoOperador = Cn.TipoOperador();
            this.comboBox5.Items.Clear();
            this.comboBox5.Text = "Seleccione Tipo de Operador";
            this.comboBox5.Items.Insert(cont, "Seleccione Tipo de Operador");
            cont++;
            foreach (String Tipo in TipoOperador)
            {
                this.comboBox5.Items.Insert(cont, Tipo);
                cont++;
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idcliente = Convert.ToInt64(this.dataGridView1.CurrentRow.Cells[0].Value);
            this.textBox2.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[1].Value);
            this.textBox3.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[2].Value);
            this.textBox4.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[3].Value);
            this.textBox5.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[4].Value);
            this.textBox6.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[5].Value);
            this.textBox7.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[6].Value);
            this.textBox8.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[7].Value);
            this.textBox9.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[8].Value);
            this.textBox10.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[9].Value);
            this.comboBox1.SelectedIndex = Convert.ToInt16(this.dataGridView1.CurrentRow.Cells[10].Value);
            this.comboBox2.SelectedIndex = Convert.ToInt16(this.dataGridView1.CurrentRow.Cells[11].Value);
            this.textBox13.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[12].Value);
            this.comboBox5.SelectedIndex = Convert.ToInt16(this.dataGridView1.CurrentRow.Cells[13].Value);
            this.textBox11.Text = "";
            this.textBox12.Text = "";
            FillGrid2();
            Existe = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox3.SelectedIndex != 0 && !this.textBox1.Text.Equals(""))
            {
                FillGrid3(this.comboBox3.SelectedIndex);
                button5_Click(sender, e);
            }
            else
            {
                FillGrid();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            guardar();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
            this.comboBox5.SelectedIndex = 0;
            this.textBox2.Text = "";
            this.textBox3.Text = "";
            this.textBox4.Text = "";
            this.textBox5.Text = "";
            this.textBox6.Text = "";
            this.textBox7.Text = "";
            this.textBox8.Text = "";
            this.textBox9.Text = "";
            this.textBox10.Text = "";
            this.textBox11.Text = "";
            this.textBox12.Text = "";
            this.textBox13.Text = "";
            this.checkBox1.Checked = false;
            this.checkBox2.Checked = false;
            this.checkBox3.Checked = false;
            this.checkBox4.Checked = false;
            this.checkBox5.Checked = false;
            this.checkBox6.Checked = false;
            this.dataGridView2.DataSource = null;
            Existe = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            guardar();
           

            if (!textBox11.Text.Equals("") && !textBox12.Text.Equals(""))
            {
                string Mail, Correo, Visita, Asistencia, Seguimiento, Lugar, Fecha;

                if (this.checkBox1.Checked == true)
                {
                    Mail = "X";
                }
                else
                {
                    Mail = null;
                }

                if (this.checkBox2.Checked == true)
                {
                    Correo = "X";
                }
                else
                {
                    Correo = null;
                }

                if (this.checkBox3.Checked == true)
                {
                    Visita = "X";
                }
                else
                {
                    Visita = null;
                }

                if (this.checkBox4.Checked == true)
                {
                    Asistencia = "X";
                }
                else
                {
                    Asistencia = null;
                }

                if (this.checkBox5.Checked == true)
                {
                    Seguimiento = "X";
                }
                else
                {
                    Seguimiento = null;
                }

                if (this.checkBox6.Checked == true)
                {
                    Fecha = this.dateTimePicker1.Text + " " + this.dateTimePicker2.Text;
                }
                else
                {
                    Fecha = null;
                }

                if (!this.textBox14.Text.Equals(""))
                {
                    Lugar = this.textBox14.Text;
                }
                else
                {
                    Lugar = null;
                }

                Cn.IngresarGestion(Convert.ToInt64(Usuario), idcliente, textBox11.Text, textBox12.Text, Mail, Correo, Visita, Asistencia, Seguimiento, Lugar, Fecha);

                this.checkBox1.Checked = false;
                this.checkBox2.Checked = false;
                this.checkBox3.Checked = false;
                this.checkBox4.Checked = false;
                this.checkBox5.Checked = false;
                this.checkBox6.Checked = false;
                this.textBox11.Text = "";
                this.textBox12.Text = "";
                this.FillGrid2();
            }
        }
        
        private void button7_Click(object sender, EventArgs e)
        {
            if (O == null)
            {
                O = new Origen();
                O.FormClosing += CierredeForm;
                O.ShowDialog();
                TipoOrigen();
            }
        }

        public void CierredeForm(object sender, EventArgs e)
        {
            O = null;
        }

        public void guardar()
        {
            if (Existe == true)
            {
                Cn.UpdateCliente(textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, comboBox1.SelectedIndex, idcliente, comboBox2.SelectedIndex, textBox13.Text);
              //  MessageBox.Show("Cliente Actualizado con Exito");
                this.FillGrid();
                this.FillGrid2();
                MessageBox.Show("Datos de Cliente Guardados con Exito");
            }
            else
            {
                if (this.comboBox1.SelectedIndex != 0 )
                {
                    if (this.comboBox2.SelectedIndex != 0)
                    {
                        if (!this.textBox2.Text.Equals(""))
                        {
                            Cn.IngresarCliente(textBox13.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, comboBox1.SelectedIndex, comboBox2.SelectedIndex);
                            Existe = true;
                            idcliente = Cn.TotalClientes();
                            this.FillGrid();
                            this.FillGrid2();
                            MessageBox.Show("Datos de Cliente Guardados con Exito");
                        }
                        else
                        {
                            MessageBox.Show("Ingrese los datos del Cliente");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Seleccione el Origen del Cliente");
                    }
                }
                else
                {
                    MessageBox.Show("Seleccione el tipo de Cliente");
                }
            }
        }

        public void Filtro1()
        {
            this.comboBox3.Text = "Seleccione Tipo de Filtro";
            this.comboBox3.Items.Insert(0, "Seleccione Tipo de Filtro");
            this.comboBox3.Items.Insert(1, "Nombre");
            this.comboBox3.Items.Insert(2, "1er Apellido");
            this.comboBox3.Items.Insert(3, "2do Apellido");
            this.comboBox3.Items.Insert(4, "Ciudad");
            this.comboBox3.Items.Insert(5, "Barrio");
            this.comboBox3.Items.Insert(6, "Tel Fijo");
            this.comboBox3.Items.Insert(7, "Tel Movil");
            this.comboBox3.Items.Insert(8, "Email");
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
